<?php
namespace App\Api;

use App\Model\ELEVENACTIVITY;
use App\Model\ELEVENRECORD;
use App\Model\ELEVENUSER;
use App\Model\VERSION;
use App\Model\WALLETIOC;
use App\Model\WALLETTRADE;
use App\Model\WALLETWITHDRAW;
use PhalApi\Api;
use PhalApi\Exception;
use PhalApi\Exception\BadRequestException;
use App\Model\DAPP;
use PhalApi\Exception\InternalServerErrorException;
use App\Model\USER;
use App\Model\WALLETADDRESS;
use App\Common\WalletBase;
use App\Common\QRcode;


/**
 * 波场钱包dapp页面接口
 *
 * @author: dogstar
 */

class App extends Api
{
    private $sort = ['null' => '全部', 'popular' => '最近流行', '0' => 'Other', '1' => 'Games', '2' => 'Exchanges', '3' => 'Collectibles', '5' => 'Gambling'];

    public function __construct()
    {
        header('Access-Control-Allow-Origin:*');
        // 响应类型
        header('Access-Control-Allow-Methods:POST');
        // 响应头设置
        header('Access-Control-Allow-Headers:x-requested-with,content-type');
        header('Access-Control-Allow-Origin:*');
    }

    public function getRules()
    {
        return
            [
                '*' => [

                ],
                'index' => [

                ],
                'detail' => [
                    'p' => ['name' => 'p', 'require' => false],
                    'page' => ['name' => 'page', 'require' => false],
                    'category' => ['name' => 'category', 'require' => true],
                ],
            ];
    }

    /**
     * dapp首页
     */
    public function index()
    {
        $model = new DAPP();
        $list['lunbo'] = $model->getLunbo();
        foreach($list['lunbo'] as &$v)
        {
            $v['sortId'] = $this->sort[$v['category']];
        }
        unset($v);
        $list['top'] = $model->getAppByTop();
        foreach($list['top'] as &$v)
        {
            $v['sortId'] = $this->sort[$v['category']];
        }
        unset($v);
        $list['list']['Other'] = $model->getApp(0);
        $list['list']['Games'] = $model->getApp(1);
        $list['list']['Exchanges'] = $model->getApp(2);
        $list['list']['Collectibles'] = $model->getApp(3);
        $list['list']['Gambling'] = $model->getApp(5);
        return $list;
    }
    /**
     * dapp分类列表
     */
    public function detail()
    {
        try
        {
            $model = new DAPP();
            $p = $this->p ? $this->p : 1;
            $page = $this->page ? $this->page : 10;
            $category = $this->category ? $this->category : 0;
            $list = $model->getAppAll($p, $category,$page);

            foreach($list as &$v)
            {
                $v['sortId'] = $this->sort[$v['category']];
                unset($v['category']);
            }
            return $list;
        }
        catch(\Exception $exception)
        {
            throw new BadRequestException('非法请求!');
        }

    }
}